package com.google.mr.okhttptest;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.Environment;
import android.os.IBinder;
import android.support.annotation.IntDef;
import android.support.annotation.Nullable;

import com.orhanobut.logger.Logger;

import java.io.IOException;

/**
 * Created by GR on 2017/12/14.
 * E-mail:android_gr@163.com
 * function:
 */

public class MusicService extends Service {

    private String[] musicDir = new String[]{
            Environment.getExternalStorageDirectory().getAbsolutePath()+"/1微信红包里的秘密(1).mp3",
            Environment.getExternalStorageDirectory().getAbsolutePath()+"/2毕业后的差距是怎么产生的.mp3",
    };

    public static MediaPlayer mediaPlayer = new MediaPlayer();

    private int musicIndex=0;

    public Binder mBind = new MyBinder();

    public class MyBinder extends Binder{
       public MusicService getService(){
            return MusicService.this;
        }
    }

    public MusicService(){
        Logger.e("musicDir[musicIndex]:"+musicDir[musicIndex]);
        if (mediaPlayer==null)return;
        try {
//            mediaPlayer.reset();
            mediaPlayer.setDataSource(Environment.getExternalStorageDirectory().getAbsolutePath()+"/1微信红包里的秘密(1).mp3");
            mediaPlayer.prepare();
            musicIndex=0;
        } catch (IOException e) {
            Logger.e("can't get to the song");
            e.printStackTrace();
        }
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return mBind;
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    public void playOrPause() {
        if (mediaPlayer.isPlaying()){
            mediaPlayer.pause();
        }else{
            mediaPlayer.start();
            Logger.e(" mediaPlayer.start()");
        }
    }

    public void stop() {
        if (mediaPlayer!=null){
            mediaPlayer.stop();
            try {
                mediaPlayer.prepare();
                mediaPlayer.seekTo(0);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public void nextMusic() {
        if (mediaPlayer!=null&&musicIndex<1){
            try {
                mediaPlayer.stop();
                mediaPlayer.reset();
                mediaPlayer.setDataSource(musicDir[musicIndex+1]);
                musicIndex++;
                mediaPlayer.prepare();
                mediaPlayer.seekTo(0);
                mediaPlayer.start();
            } catch (IOException e) {
                Logger.e("can't jump next music");
                e.printStackTrace();
            }
        }
    }

    public void previousMusic() {
        if (mediaPlayer!=null&&musicIndex>0){
            try {
                mediaPlayer.stop();
                mediaPlayer.reset();
                mediaPlayer.setDataSource(musicDir[musicIndex-1]);
                musicIndex--;
                mediaPlayer.prepare();
                mediaPlayer.seekTo(0);
                mediaPlayer.start();
            } catch (IOException e) {
                Logger.e("can't jump previous music");
                e.printStackTrace();
            }
        }
    }
}
